package com.bt.springboot.web.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bt.springboot.common.result.Result;
import com.bt.springboot.common.util.RegexUtils;
import com.bt.springboot.entity.User;
import com.bt.springboot.web.dao.UserMapper;
import com.bt.springboot.web.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * 服务实现类
 *
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Override
	public Result sendCode(String phone, HttpSession session) {
		if (RegexUtils.isPhoneInvalid(phone)){
			return Result.failure("手机号格式错误！");
		}
		// 生成验证码
		String code = RandomUtil.randomNumbers(6);
		log.info("发送短信验证码成功 验证码：{}",code);
		return Result.success(1,"成功");
	}

	@Override
	public void updateUser() {
		LambdaUpdateWrapper<User> lambdaUpdateWrapper = Wrappers.<User>lambdaUpdate()
				.set(User::getPassword, "123456");
		this.update(lambdaUpdateWrapper);
	}
}
